/*
 * TComPtr.h
 *
 *  Created on: 2011-8-5
 *      Author: terry
 */

#ifndef TCOMPTR_H_
#define TCOMPTR_H_




template < typename T >
class TComPtr
{
public:
    explicit TComPtr(T* ptr = NULL):
        m_ptr(ptr)
    {
    }

    ~TComPtr()
    {
        cleanup();
    }

    T* operator->() const
    {
        return get();
    }

    operator bool() const
    {  // check wrapped pointer for NULL
        return m_ptr != NULL;
    }

    T* get() const
    {  // return wrapped pointer
        return m_ptr;
    }

    T** operator & ()
    {
        return &m_ptr;
    }

    void reset(T* p = NULL)
    {
    	cleanup();
        m_ptr = p;
    }

private:
	// disabled
	TComPtr(const TComPtr& other);
	TComPtr& operator = (const TComPtr& other);

	void cleanup()
	{
		if (m_ptr)
		{
			m_ptr->Release();
			m_ptr = NULL;
		}
	}

protected:
    T*  m_ptr;
};




#endif /* TCOMPTR_H_ */
